<template>
  <div v-if="visible" class="overlay" @click.self="handleOverlayClick">
    <div :class="['popup', { fullscreen: isFullscreen }]" :style="popupStyle">
      <!-- 弹窗头部 -->
      <div class="popup-header">
        <div class="fullscreen-btn" @click="toggleFullscreen">
          <span
            class="iconfont"
            :class="[isFullscreen ? 'icon-banping' : 'icon-quanping']"
          ></span>
        </div>
        <div class="close-btn" @click="handleClose">
          <span class="iconfont icon-guanbi"></span>
        </div>
      </div>

      <!-- 弹窗主体内容 -->
      <div class="popup-body">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    width: {
      type: Number,
      default: 600, // 默认宽度
    },
    height: {
      type: Number,
      default: 400, // 默认高度
    },
  },
  data() {
    return {
      isFullscreen: false,
    };
  },
  computed: {
    popupStyle() {
      const width = this.isFullscreen ? "100vw" : `${this.width}px`;
      const height = this.isFullscreen ? "100vh" : `${this.height}px`;
      return { width, height };
    },
  },
  methods: {
    // 点击遮罩层时关闭弹窗
    handleOverlayClick() {
      this.closePopup();
    },
    // 关闭弹窗
    handleClose() {
      this.closePopup();
    },
    // 关闭弹窗，通知父组件
    closePopup() {
      this.$emit("update:visible", false); // 触发父组件更新 `visible` 状态
      this.isFullscreen = false;
    },
    // 切换全屏模式
    toggleFullscreen() {
      this.isFullscreen = !this.isFullscreen;
    },
  },
};
</script>

<style scoped>
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.popup {
  position: relative;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  transition: all 0.3s ease;
  overflow: hidden;
}

.popup.fullscreen {
  border-radius: 0;
  width: 100vw !important;
  height: 100vh !important;
}

.popup-header {
  display: flex;
  justify-content: flex-end;
  padding: 10px 15px; /* 减少内边距 */
  flex-shrink: 0;
}

.close-btn,
.fullscreen-btn {
  width: 25px; /* 减小按钮宽度 */
  height: 25px; /* 减小按钮高度 */
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.1); /* 增加对比度 */
  color: #333; /* 更改文字颜色 */
  font-size: 14px; /* 减小字体大小 */
  font-weight: bold;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.2s;
  margin-left: 10px; /* 减少按钮间距 */
}

.close-btn:hover,
.fullscreen-btn:hover {
  background: rgba(0, 0, 0, 0.2); /* 悬停时的背景色 */
  transform: scale(1.05); /* 减少放大效果 */
}

.iconfont {
  font-size: 13px; /* 减小图标大小 */
}

.popup-body {
  flex: 1;
  padding: 20px;
  overflow: auto;
  display: flex;
  flex-direction: column;
}

/* 隐藏滚动条 */
.popup-body::-webkit-scrollbar {
  width: 4px; /* 设置滚动条的宽度 */
}

/* 隐藏滚动条轨道外层 */
.popup-body::-webkit-scrollbar-track {
  display: none; /* 完全隐藏滚动条轨道 */
}

/* 自定义滚动条滑块 */
.popup-body::-webkit-scrollbar-thumb {
  background-color: #888; /* 滑块背景颜色 */
  border-radius: 2px; /* 滑块圆角 */
  border: none; /* 去掉滑块边框 */
}

/* 去掉滚动条轨道（外部包裹） */
.popup-body::-webkit-scrollbar-track-piece {
  background-color: transparent; /* 轨道背景颜色 */
}
</style>
